import { uploadWxImage } from '@/api/upload';
import { alertDialog } from '@/utils/functions';
import { isRef } from 'vue';

/**
 * vant 使用微信上传图片方式（不适用，作废）
 *
 * @param value 接收vant显示的v-model图片数组
 * @param options.beforeUpload 由于vant需要在上传前显示图片加载占位，需要增加可判断hook
 * @param options.uploaded 上传完毕后触发hook
 */
export function useWechatUploaderEngine (value, options = {}) {
  const { beforeUpload, uploaded, count = 1 } = options;
  uploadWxImage({
    count,
    beforeUpload (files = []) {
      console.log('serverids', files);
      const images = files.map(file => {
        file.type = 'image';
        file.status = 'uploading';
        file.message = '上传中...';
        file.isImage = true;
        // 预览使用file.content，已在wx中处理
        file.url = file.localId;
        if (isRef(value)) {
          value.value.push(file);
        } else {
          value.push(file);
        }
        return file;
      });
      beforeUpload && beforeUpload();
      return images;
    },
    onComplete (files) {
      console.log('upload done.', files);
      files.map(file => {
        file.url = file.file.url;
        file.status = '';
        file.message = '';
      });
      uploaded && uploaded(files);
    },
    onError (error) {
      alertDialog(error.message);
    }
  });
}
